package com.topnav.mc.admin.service.impl;

import java.util.Date;
import java.util.List;

import com.topnav.mc.admin.entity.bean.UserRoleBean;
import com.topnav.mc.admin.entity.param.UserRoleParam;
import com.topnav.mc.base.mc.McServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.lib.exception.CustomException;
import com.topnav.mc.admin.dao.mapper.UserRoleMapper;
import com.topnav.mc.admin.entity.UserRole;
import com.topnav.mc.admin.service.IUserRoleService;
import com.topnav.mc.base.CommServiceImpl;
@Service
public class UserRoleServiceImpl extends McServiceImpl<UserRoleMapper,UserRole, UserRoleBean, UserRoleParam> implements IUserRoleService{
	@Override
	public boolean beforeSave(UserRole entity){
		if (this.getOne(new QueryWrapper<UserRole>().eq("user_id",entity.getUserId()).eq("role_id", entity.getRoleId()), true) != null){
			String[] objs = new String[]{entity.getUserId().toString(),entity.getRoleId().toString()};
			throw new CustomException(-1,"userId  %s  + orgId %s 已存在",objs);
		}
		return true;
	}

	@Override
	public boolean beforeUpdate(UserRole entity){
		QueryWrapper<UserRole> wrapper = new QueryWrapper<UserRole>()
				.eq("user_id",entity.getUserId()).eq("role_id", entity.getRoleId()).ne("id", entity.getId());
		if (this.getOne(wrapper, true) != null){
			throw new CustomException(-1,"userId  %s  + orgId %s 已存在",new String[]{entity.getUserId().toString(),entity.getRoleId().toString()});
		}
		entity.setUpdateTime(new Date());
		return true;

	}

	@Override
	public boolean beforeDelete(UserRole entity){
		return true;
	}

    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public List<UserRole> list(Wrapper<UserRole> queryWrapper) {
		QueryWrapper<UserRole> wrapper = new QueryWrapper<UserRole>();
		if (queryWrapper.getEntity() != null){
			if (queryWrapper.getEntity().getId()!=null){
				wrapper.eq("id", queryWrapper.getEntity().getId());
			}

			if (!StringUtils.isEmpty(queryWrapper.getEntity().getUserId())){
				wrapper.eq("user_id", queryWrapper.getEntity().getUserId());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getRoleId())){
				wrapper.eq("role_id", queryWrapper.getEntity().getRoleId());
			}
		}


        return getBaseMapper().selectList(wrapper);
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public <E extends IPage<UserRole>> E page(E page, Wrapper<UserRole> queryWrapper) {
		QueryWrapper<UserRole> wrapper = new QueryWrapper<UserRole>();
		if (queryWrapper.getEntity().getId()!=null){
			wrapper.eq("id", queryWrapper.getEntity().getId());
		}
		if (!StringUtils.isEmpty(queryWrapper.getEntity().getUserId())){
			wrapper.eq("user_id", queryWrapper.getEntity().getUserId());
		}
		if (!StringUtils.isEmpty(queryWrapper.getEntity().getRoleId())){
			wrapper.eq("role_id", queryWrapper.getEntity().getRoleId());
		}
        return getBaseMapper().selectPage(page, wrapper);
    }
}
